import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export const createStore = () => {
  return new Vuex.Store({
    state: () => ({
      posts: []
    }),
    mutations: {
      setPosts (state, data) {
        state.posts = data
      }
    },
    actions: {
      async getPosts({ commit }) {
        // 在服务端渲染期间务必让 action 返回一个 Promise
        try {
          const { data } = await axios.get('https://cnodejs.org/api/v1/topics')
          commit('setPosts', data.data)
        } catch (error) {
          commit('setPosts', [
            {
              id: 1,
              title: '1'
            }
          ])
        }
      }
    }
  })
}